# -*- coding: cp936 -*-

'ÔÚÆÁÄ»ÏÔÊ¾¾®×ÖÆå´°¿Ú'
import pygame,random,os
from random import randint
from pygame.locals import*

pygame.init()   
os.environ["SDL_VIDEO_CENTERED"]="1"


width, height = 550, 550
screen = pygame.display.set_mode((width, height))  
red = (255,0,0)
blue = (0,0,255)
white = (255,130,203)
black = (255,223,110)
empty = 0
x = -1
o = 1
board = [[empty] * 3,[empty] * 3,[empty] * 3] 
beijing = pygame.image.load("11.jpg")

def draw_game(): 
        pygame.draw.rect(screen, white, (0, 0, width, height))
        pygame.draw.line(screen, black, (180, 0), (180, 550), 5)
        pygame.draw.line(screen, black, (360, 0), (360, 550), 5)
        pygame.draw.line(screen, black, (0, 180), (550, 180), 5)
        pygame.draw.line(screen, black, (0, 360), (550, 360), 5)


        font = pygame.font.Font(None,24)
        minutes = str(pygame.time.get_ticks()/60000)
        seconds = str(pygame.time.get_ticks()/1000%60).zfill(2)
        clock = font.render(minutes+':'+seconds,True,(0,0,0))
        screen.blit(clock,(500,10))

        for row,line in enumerate(board):  
            for col,val in enumerate(line):
                if val == x:
                    
                    upper_left = (col * 180 + 5, row * 180 + 5)
                    lower_right = (col * 180 + 175, row * 180 + 175)
                    pygame.draw.line(screen, red, upper_left, lower_right, 5)
                    upper_right = (col * 180 + 175, row * 180 + 5)
                    lower_left = (col * 180 + 5, row * 180 + 175)
                    pygame.draw.line(screen, red, upper_right, lower_left, 5)
                elif val == o:
                    rect = (col * 180 + 5, row * 180 + 5 ,170,170)
                    pygame.draw.ellipse(screen,blue,rect,5) 
                    
        pygame.display.flip()
        
def draw_o():
    while True:
        row = randint(0,2)
        col = randint(0,2)
        if board[row][col] == 0:   
            board[row][col] = 1  
            break
    draw_game()
    pygame.display.flip()
                      
   

while True:  
      for event in pygame.event.get():  

          pos = None 
          
          temp = 0
          
          if event.type == pygame.QUIT:  
                    pygame.quit()        
                    exit(0)   
          elif event.type == MOUSEBUTTONDOWN :    
        
               draw_game()
               pygame.display.flip()  
               
               pos = (event.pos[1] / 180,event.pos[0] / 180) 
               row,col = pos
               board[row][col] = -1
               draw_game()
               for i in range(3):
                   for j in range(3):
                       if board[i][j] == 0:
                           temp += 1
               print temp
               if temp >= 2:
                   draw_o()
